using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using Core.SimpleTemp.Entitys;

namespace Core.SimpleTemp.Api.PC.Api
{
    /// <summary>
    /// PX_Exam
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class PX_ExamController : CoreApiController
    {
        private readonly PX_ExamService _service;
        /// <summary>
        /// PX_Exam
        ///</summary>
        public PX_ExamController(PX_ExamService service)
        {
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<PX_ExamDto>
                pagingQueryModel)
        {
            IPageModel<PX_ExamDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(PX_ExamDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(model);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        //[HttpPost("Export")]
        //[PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.Export),AuthorizationConst.EXPORT)]
        //public async Task<IActionResult>
        //    Export(PagingModel<PX_ExamDto>
        //        pagingQueryModel)
        //        {
        //        var fileName = await _service.Export(pagingQueryModel);
        //        if (string.IsNullOrWhiteSpace(fileName))
        //        return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
        //        return this.JsonSuccess(fileName);
        //        }
        #endregion


        #region 考试接口

        /// <summary>
        /// 参加考试
        ///</summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("cjks")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.cjks), "参加考试")]
        public async Task<IActionResult> cjks(Guid id, String type)
        {

            var result = await _service.cjks(id, type);
            if (result == null)
                return this.JsonFaild("您已经参加了5次考试，无法继续参加考试！", null);
            else
                return this.JsonSuccess(result);

        }
        /// <summary>
        /// 切换题目
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("lastornextAsync")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.lastornextAsync), "切换题目")]
        public async Task<IActionResult> lastornextAsync(PX_ExamUserQuestionsDto dto)
        {

            await _service.lastornext(dto);

            return this.JsonSuccess();

        }

        /// <summary>
        /// 提交试卷
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("tjsj")]
        [PermissionFilter(nameof(PX_ExamController) + "_" + nameof(PX_ExamController.tjsj), "提交试卷")]
        public async Task<IActionResult> tjsj(PX_ExamUserQuestionsDto dto)
        {
            Guid result = await _service.tjsj(dto);
            return this.JsonSuccess(result);

        }




        #endregion


    }
}
